<script>
import { reactive } from "@vue/reactivity";
import { useRouter } from "vue-router";
import { public_status } from "../store/public_status";
import { useStore } from "../store/index";
export default {
  setup() {
    // # 隐藏底部导航
    public_status().display = false;

    let data = reactive({
      del_btn_isShow: false,
    });

    // # 用户数据
    let userId = localStorage.getItem("P4_userId");
    let userCollect = useStore().userData[userId].Favorites;
    console.log(userCollect);

    let showdel = () => {
      data.del_btn_isShow = !data.del_btn_isShow;
    };

    let del = (id) => {
      let index = userCollect.findIndex((item) => item.id == id);
      userCollect.splice(index, 1);
    };

    // # 配置路由
    let router = useRouter();
    return {
      data,
      router,
      userCollect,

      showdel,
      del,
    };
  },
};
</script>

<template>
  <div class="collectView">
    <!-- 头部 -->
    <header class="header">
      <div class="back" @click="router.back()">
        <i class="iconfont icon-arrow-left"></i>
      </div>
      <div class="title">Collect</div>
      <div class="control" @click="showdel">管理</div>
    </header>
    <!-- 头部 -->

    <!-- 收藏列表 -->
    <div class="collectBox">
      <div class="content">
        <router-link
        :to="'/details?id=' + item.id"
          :class="['item', { del: data.del_btn_isShow }]"
          v-for="item in userCollect"
          :key="item.id"
        >
          <div class="img">
            <img :src="item.imgUrl" alt="" />
          </div>

          <div class="text">
            <div class="name">{{ item.title }}</div>
            <div class="price">￥{{item.price}}</div>
          </div>

          <div class="del" @click="del(item.id)">删除</div>
        </router-link>
      </div>
    </div>
    <!-- 收藏列表 -->

    <!-- 空状态 -->
    <div class="Empty_state" v-show="userCollect.length <= 0">
      <img src="../assets/imgs/暂无内容.svg" alt="" />
      <router-link to="/home" class="btn">去逛逛</router-link>
    </div>
    <!-- 空状态 -->
  </div>
</template>

<style lang="scss" scoped>
.collectView {
  // todo 头部
  > header.header {
    height: 56px;
    line-height: 56px;
    text-align: center;

    background: #fff;

    position: sticky;
    top: 0;
    z-index: 10;

    > .back {
      position: absolute;
      left: 15px;
      top: 0;
      bottom: 0;
      margin: auto;

      width: 37px;
      height: 37px;
      line-height: 37px;
      border-radius: 50%;
      background: #f9f9f9;

      > i {
        text-align: center;
      }
    }

    > .title {
      font-size: 18px;
      font-weight: bold;
    }

    > .btn {
      position: absolute;
      top: 0;
      bottom: 0;
      right: 15px;
      margin: auto;

      padding: 0 15px;
      height: 32px;
      line-height: 32px;
      text-align: center;

      border-radius: 15px;
      background: #f9f9f9;
    }
    .control {
      position: absolute;
      right: 15px;
      top: 0;
      bottom: 0;
      margin: auto;
      font-size: 14px;
      height: 37px;
      line-height: 37px;
      border-radius: 10px;
      font-weight: 600;
      padding: 0 10px;
      background: #f9f9f9;
    }
  }

  // todo 收藏列表
  .collectBox {
    padding: 12px;

    > .content {
      width: 100%;
      overflow: hidden;

      > .item {
        margin: 15px 0px;
        height: 76px;

        display: flex;
        align-items: center;

        position: relative;
        z-index: 100;
        transition: all 0.3s;

        background: #f9f9f9;
        border-radius: 10px;

        &.del .del {
          width: 76px;
          font-size: 14px;
        }

        > .img {
          width: 76px;
          flex-shrink: 0;

          > img {
            width: 100%;
            border-radius: 10px;
          }
        }

        > .text {
          flex: 1;
          margin-left: 5px;
          padding: 18px 0;

          > .name {
            height: 18px;
            font-size: 16px;
            line-height: 18px;
            font-weight: bold;
            margin-bottom: 5px;
            margin-right: 20px;

            overflow: hidden;
          }

          > .price {
            color: #ccc;
          }
        }

        > .del {
          width: 0px;
          height: 100%;

          transition: all 0.3s;

          border-top-right-radius: 20px;
          border-bottom-right-radius: 20px;

          background: #f34d5c;

          flex-shrink: 0;

          font-size: 10px;
          line-height: 76px;
          color: #fff;
          text-align: center;
          font-weight: bold;
        }
      }
    }
  }

  // todo 空状态
  .Empty_state {
    max-width: 400px;
    margin: 0 auto;

    position: relative;

    > .btn {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 50px;
      margin: 0 auto;

      background: #333;
      border-radius: 10px;
      color: #fff;

      width: 80px;
      height: 30px;

      line-height: 30px;
      text-align: center;

      z-index: 10;

      &:active {
        text-decoration: underline;
        transform: translateY(-1px);
      }
    }
  }
}
</style>